package com.peza.mines.view;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

import com.peza.mines.domain.Board;
import com.peza.mines.domain.BoardResponse;
import com.peza.mines.domain.Field;
import com.peza.mines.domain.FieldClass;
import com.peza.mines.domain.GameGrid;
import com.peza.mines.logic.Logic;
import com.peza.mines.logic.LogicImpl;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JToolBar;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import org.apache.log4j.Logger;

public class MineView implements ActionListener{

	private JFrame frame;
	
	private JPanel buttonpanel;
	
	private Logic logic;
	
	private static Board board;
	
	private JButton [][] grid;
			
	private final static int SIZE = 4;
	
	private final static int MINES = 1;
	
	private final Logger logger = Logger.getLogger(this.getClass());

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MineView window = new MineView();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MineView() {
		initialize();
	}
	
	
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 346);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
		//frame.getContentPane().setLayout(layout);
		
		
		
		GridLayout layout = new GridLayout(SIZE, SIZE, 1, 1);
		buttonpanel = new JPanel();
		buttonpanel.setLayout(layout);
		
		
		
		frame.getContentPane().add(buttonpanel);
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu gameMenu = new JMenu("Game");
		menuBar.add(gameMenu);
		
		JMenuItem mntmRestartitem = new JMenuItem("Restart");
		mntmRestartitem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				logger.debug("restarting");
				
			}
		});
		gameMenu.add(mntmRestartitem);
		initBoard();
	}
	
	
	public void actionPerformed(ActionEvent event) {
		
		Object src = event.getSource();
		if (src instanceof JButton){
			JButton button = (JButton) src;
			int id = Integer.parseInt(button.getName());
			Field field = board.getFieldById(id);
			System.out.println("ACTION=click - ID=" +id + " - FIELD=" + field.getYpos() +":"+field.getxPos());
			board = logic.click(field);
			buildBoard();
		}	
	}
	
	public void buildBoard(){
		/** debug **/
		logger.debug("BUILDBOARD: \n" + board.toString());
		
		/** create the buttons **/
		Field [][] fields = board.getFields();
		int size = fields.length;
		
		for (int y = 0; y < size; y++){
			for (int x = 0; x < size; x++){
				Field field = fields[y][x];
				JButton button = findButton(y,x);
				logger.debug("BUTTON = " + button.getName());
				if (field.isVisible()){
					button.setEnabled(false);
					button.setForeground(Color.GREEN);
					button.setText(Integer.toString(field.getProx()));
				} else {
					button.setText("");
				}
			}
		}
	}
	
	private void initBoard(){	
		/** create the board **/
		board = new Board(SIZE,MINES);
		logic = new LogicImpl(board);
		Field [][] fields = board.getFields();
		int size = fields.length;
		grid = new JButton [size][size];
		for (int y = 0; y < size; y++){
			for (int x = 0; x < size; x++){
				Field field = fields[y][x];
				JButton button = new JButton();
				button.setName(Integer.toString(field.getId()));
				button.addActionListener(this);
				buttonpanel.add(button);
				grid[y][x] = button;
			}
		}
	}
	
	private JButton findButton(int y, int x){
		JButton button = grid[y][x];
		return button;
	}
}
